# Copyright (c) 2023 Broin All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

OUTPUT := .output
CLANG ?= clang
LLVM_STRIP ?= llvm-strip
BPFTOOL ?= $(abspath ../../tools/bpftool)
LIBBPF_SRC := $(abspath ../../libbpf/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' 		\
			 | sed 's/aarch64/arm64/' 	\
			 | sed 's/ppc64le/powerpc')

VMLINUX := ../../vmlinux/$(ARCH)/vmlinux.h

# Use customed libbpf api & uapi
INCLUDES := -I./ -I$(OUTPUT) -I../../libbpf/include/uapi -I../include -I$(dir $(VMLINUX)) 
CFLAGS := -g -Wall -Wno-unused-function -fsanitize=address

POLICY_PATH ?= ../../config/policy
LOG_LEVEL ?= DEBUG
LOG_PATH ?= ../../log

APPS = lsm_permission

# Get Clang's default includes on this system. We'll explicitly add these dirs
# to the includes list when compiling with `-target bpf` because otherwise some
# architecture-specific dirs will be "missing" on some architectures/distros -
# headers such as asm/types.h, asm/byteorder.h, asm/socket.h, asm/sockios.h,
# sys/cdefs.h etc. might be missing.
#
# Use '-idirafter': Don't interfere with include mechanics except where the
# build would have failed anyways.
CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \
	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')

ifeq ($(V), 1)
	Q = INCLUDES := -I$(OUTPUT) -I../../libbpf/include/uapi -I$(dir $(VMLINUX))
	msg =
else
	Q = @
	msg = @printf '  %-8s %s%s\n'					\
		      "$(1)"						\
		      "$(patsubst $(abspath $(OUTPUT))/%,%,$(2))"	\
		      "$(if $(3), $(3))"
	MAKEFLAGS += --no-print-directory
endif

.PHONY:all
all: config.yaml $(APPS)

.PHONY:clean
clean:
	$(call msg,CLEAN)
	$(Q)rm -rf $(OUTPUT) $(APPS)
	$(Q)rm -rf ../../config/config.yaml

$(OUTPUT) $(OUTPUT)/libbpf:
	$(call msg,MKDIR,$@)
	$(Q)mkdir -p $@

# Build bpf
$(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT)/libbpf
	$(call msg,LIB,$@)
	$(Q)$(MAKE) -C $(LIBBPF_SRC) BUILD_STATIC_ONLY=1	\
		    OBJDIR=$(dir $@)/libbpf DESTDIR=$(dir $@)	\
		    INCLUDEDIR= LIBDIR= UAPIDIR=		\
		    install

# Compile bpf code
$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(VMLINUX) | $(OUTPUT)
	$(call msg,BPF,$@)
	$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) -c $(filter %.c,$^) -o $@
	$(Q)$(LLVM_STRIP) -g $@ # strip useless DWARF info

# Generate bpf skeletons
$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT)
	$(call msg,GEN-SKEL,$@)
	$(Q)$(BPFTOOL) gen skeleton $< > $@

# Compile user-space code
$(patsubst %, $(OUTPUT)/%.o, $(APPS)): %.o: %.skel.h

$(OUTPUT)/%.o: %.c $(wildcard %.h) | $(OUTPUT)
	$(call msg,CC,$@)
	$(Q)$(CC) $(CFLAGS) $(INCLUDES) -lcjson -lyaml -c $(filter %.c, $^) -o $@

# Build binary
$(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT)
	$(call msg,BINARY,$@)
	$(Q)$(CC) $(CFLAGS) $^ -lcjson -lyaml -lelf -lz -o $@

config.yaml: config_template.yaml
	@echo "Generrating config.yaml..."
	sed -e "s|{{POLICY_PATH}}|$(POLICY_PATH)|" -e "s|{{LOG_LEVEL}}|$(LOG_LEVEL)|" -e "s|{{LOG_PATH}}|$(LOG_PATH)|" config_template.yaml > ../../config/config.yaml
	cp -a ./policy/* $(POLICY_PATH)

# delete failed targets
.DELETE_ON_ERROR:

# Keep intermediate (.skel.h, .bpf.o ...) targets
.SECONDARY: